package com.hqj.client.controller;

import com.hqj.client.entity.DownloadForm;
import com.hqj.client.service.DownloadFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 王建林
 * @version 1.0
 * @date 2020/10/19
 */
@Api(tags = "资料下载")
@Controller
@RequestMapping("/DownloadFormController")
public class DownloadFormController {
    @Resource
    DownloadFormService downloadFormService;

    @ApiOperation("分页查询")
    @GetMapping("/findPage/{page}/{size}")
    public String findPage(@PathVariable int page, @PathVariable int size, Model model) {
        List<DownloadForm> page1 = downloadFormService.findPage(page, size);
        if (page1.isEmpty()){
            model.addAttribute("msg", "分页查询失败");
            return "/page/BlogIndex";
        }else {
            model.addAttribute("msg", "分页查询成功");
            return "/page/BlogIndex";
        }
    }


    @ApiOperation("增加")
    @PostMapping("/add")
    public String add(@RequestBody DownloadForm downloadForm, Model model) {
        int add = downloadFormService.add(downloadForm);
        if (add > 0) {
            model.addAttribute("msg", "添加成功");
        }
        return "/page/BlogIndex";
    }

    @ApiOperation("修改")
    @PutMapping("/update")
    public String update(@RequestBody DownloadForm downloadForm, Model model) {
        int update = downloadFormService.update(downloadForm);
        if (update > 0) {
            model.addAttribute("msg", "修改成功");
        }
        return "/page/BlogIndex";
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable Long id, Model model) {
        int delete = downloadFormService.delete(id);
        if (delete > 0) {
            model.addAttribute("msg", model);
        }
        return "/page/BlogIndex";
    }

    @ApiOperation("搜索")
    @GetMapping("/search")
    public String search(@RequestParam @ApiParam("文件名称") String fileNames, Model model){
        List<DownloadForm> search = downloadFormService.search(fileNames);
        if (search.isEmpty()){
            model.addAttribute("msg","未搜索到");
            return "/page/BlogIndex";
        }else {
            model.addAttribute("msg","搜索成功");
            return "/page/BlogIndex";
        }
    }

}
